package com.nbicc.file.controller;

import com.nbicc.common.ResponseObj;
import com.nbicc.file.service.impl.FileServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ApiIgnore()
@Api(value = "文件系统" , description = "文件系统中，文件的基础操作")
@Controller
@RequestMapping(value = "/file")
public class FileController {

    @Resource(name = "FileServiceImpl")
    private FileServiceImpl fileService;

    @GetMapping("/{id}")
    public void downloadFile(
            @PathVariable("id") long id,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        fileService.downloadFile(request , response , id);
    }

    @PostMapping
    public void uploadFile(
            HttpServletRequest request,
            MultipartFile uploadFile) throws IOException {
        fileService.uploadFile(request , uploadFile);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseObj deleteFile(@PathVariable long id) throws IOException {
        fileService.deleteFile(id);
        return ResponseObj.success();
    }

}
